package com.example.car

import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.widget.EditText
import androidx.activity.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.car.adapter.CarAdapter
import com.example.car.databinding.ActivityCarBinding
import com.example.car.entity.Car
import com.example.car.entity.PostCar
import com.example.car.vm.CarViewModel
import com.example.common.view.view.BaseMvvmActivity
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat

@Route(path = "/car/CarActivity")
@AndroidEntryPoint
class CarActivity : BaseMvvmActivity<ActivityCarBinding>() {
    private val carViewModel:CarViewModel by viewModels()
    var index:Int = 0
    lateinit var edText:Array<EditText>
    override fun initView() {
        edText = arrayOf(mDataBinding.et1,mDataBinding.et2,mDataBinding.et3,mDataBinding.et4,mDataBinding.et5,mDataBinding.et6,mDataBinding.et7,mDataBinding.et8)
        mDataBinding.tvTime.text = SimpleDateFormat("2026-MM-dd").format(System.currentTimeMillis())
        for (i in edText.indices){
            edText[i].addTextChangedListener(object :TextWatcher{
                override fun beforeTextChanged(
                    s: CharSequence?,
                    start: Int,
                    count: Int,
                    after: Int
                ) {}

                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}

                override fun afterTextChanged(s: Editable?) {
                    if(s?.length == 1){
                        if(index<edText.size-1){
                            index++
                            edText[index].requestFocus()
                        }
                        if(index == edText.size-1){
                            mDataBinding.tvCar.text = mDataBinding.et1.text.toString()+mDataBinding.et2.text.toString()+mDataBinding.et3.text.toString()+mDataBinding.et4.text.toString()+mDataBinding.et5.text.toString()+mDataBinding.et6.text.toString()+mDataBinding.et7.text.toString()+mDataBinding.et8.text.toString()
                        }
                    }else if(s?.length == 0){
                        if(index>0){
                            edText[index-1].requestFocus()
                         }
                    }
                }
            })
        }
        mDataBinding.btnAddCar.setOnClickListener {
            if(mDataBinding.tvCar.text.isEmpty()){
                ToastUtils.showShort("请输入车牌号")
                return@setOnClickListener
            }else{
                carViewModel.postCar(PostCar(2,mDataBinding.tvCar.text.toString(),"水豚噜噜",mDataBinding.et1.text.toString(),5))
            }
        }
    }

    override fun loadData() {
        carViewModel.getCar()
    }

    override fun observeDataChange() {
        carViewModel.mCarSuccess.observe(this){
            mDataBinding.rvCar.run {
                layoutManager = GridLayoutManager(this@CarActivity,7)
                adapter = CarAdapter(it)
                    .apply { notifyDataSetChanged() }
                    .apply {
                        setItemOnClickListener(object :CarAdapter.ItemOnClickListener{
                            override fun onClick(view: View, position: Int) {
                                edText[0].setText(it[position].toString().substring(17,18))
                            }
                        })
                    }
            }
        }
        carViewModel.mPostCarSuccess.observe(this){
            ToastUtils.showShort("添加成功")
            ARouter.getInstance().build("/car/CarPayActivity")
                .withString("carCode",mDataBinding.tvCar.text.toString()).navigation()
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_car

}